package com.daocao.hua.activity;

import android.annotation.TargetApi;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.api.callback.ChangePhoneContract;
import com.daocao.hua.api.requestInfo.ChangePhoneRequest;
import com.daocao.hua.constant.Const;
import com.daocao.hua.model.ModifyPhoneCheckCodeModel;
import com.daocao.hua.model.ResponseModel;
import com.daocao.hua.util.PreferenceUtils;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.UI;
import com.daocao.hua.view.CircularProgress;
import com.daocao.hua.view.Dialog;
import com.daocao.hua.view.SendCodeDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-11-15.
 */

public class ChangPhoneSecActivity extends BaseActivity implements ChangePhoneContract.View {


    @BindView(R.id.edit_phone)
    EditText editPhone;


    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.progressBar)
    CircularProgress loading;
    private Unbinder unbiner;
    private ChangePhoneRequest api;
    private String token;
    private SendCodeDialog dialog;
    private String phoneNew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_change_phone_sec);
        unbiner = ButterKnife.bind(this);
        baseInitView(this, "ChangPhoneSecActivity", Const.ViewType.ACTIVITY);
        initBar();
        init();
    }

    private void init() {
        token = getIntent().getStringExtra("token");
//        setBackIntent();
        getLeftImgBtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
        setTitleText("绑定新手机号");
        api = ChangePhoneRequest.create();
        api.attachView(this);
        dialog = new SendCodeDialog(this);
        dialog.setDialogClickListener(new SendCodeDialog.DialogDefaultClickListener() {
            @Override
            public void confirm(String code) {
                api.getModifyphone(phoneNew, code, token);
            }

            @Override
            public void cancel() {
                resetBtn();
                dialog.dismiss();
            }

            @Override
            public void sendCode() {
                api.getCode("sms", phoneNew);
            }
        });
    }

    private CountDownTimer timer = new CountDownTimer(60000L, 1000L) {
        public void onFinish() {
            resetBtn();
        }

        @TargetApi(16)
        public void onTick(long paramAnonymousLong) {
            if (dialog.isShowing()) {
                dialog.setCodeText(paramAnonymousLong / 1000L + "s", false);
            }
        }
    };

    @TargetApi(16)
    private void resetBtn() {
        if (this.timer != null)
            this.timer.cancel();

        if (dialog.isShowing()) {
            dialog.setCodeText("发送", true);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        api.detachView();
        api = null;
        unbiner.unbind();
        unbiner = null;
    }


    @OnClick({R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.btn_send_code:
//                if (TextUtils.isEmpty(UI.toString(editPhone))) {
//                    ToastUtil.makeText(this, "手机号不能为空").show();
//                    return;
//                }
//                api.getCode("sms", UI.toString(editPhone));
//                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editPhone))) {
                    ToastUtil.makeText(this, "手机号不能为空").show();
                    return;
                }
                if (editPhone.getText().toString().length() < 11) {
                    ToastUtil.makeText(this, getResources().getString(R.string.phone_is_error), 0).show();
                    return;
                }

                phoneNew = UI.toString(editPhone);
                dialog.setContent(phoneNew);
                dialog.show();

//                if (TextUtils.isEmpty(UI.toString(editCode))) {
//                    ToastUtil.makeText(this, "验证码不能为空").show();
//                    return;
//                }
//                api.getModifyphone(UI.toString(editPhone), UI.toString(editCode), token);
                break;
        }
    }

    private void showLoading(boolean b) {
        if (loading != null) {
            if (b)
                loading.setVisibility(View.VISIBLE);
            else
                loading.setVisibility(View.GONE);
        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getModifyphoneSendcode(ResponseModel item) {

    }

    @Override
    public void getModifyphoneCheckcode(ModifyPhoneCheckCodeModel item) {

    }

    @Override
    public void getModifyphone(ResponseModel item) {
        resetBtn();
        dialog.dismiss();
        PreferenceUtils.putString(this, "mobile", phoneNew);
        ToastUtil.makeText(this, "修改成功").show();
        finish();
    }

    @Override
    public void getCodeSuccess(ResponseModel item) {
        timer.start();
        ToastUtil.makeText(this, "已发送验证码，注意查收").show();
    }

    private void showDialog() {
        Dialog.showDefaultDialog(this, "提示", "确定放弃修改手机号吗？", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {
                finish();
            }

            @Override
            public void cancel() {

            }
        });
    }

    @Override
    public void onBackPressed() {
        showDialog();
    }
}
